package com.xyre.client.business.index.utils;

import android.util.Log;

import com.google.gson.Gson;
import com.xyre.client.business.index.bean.RegistResponse;
import com.xyre.client.business.index.bean.RegistRequest;
import com.xyre.client.business.index.bean.VerifyCodeRequest;
import com.xyre.client.business.index.bean.VerifyCodeResponse;
import com.xyre.client.business.index.presenter.IGuidesPresenter;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * ==========================================
 * <p/>
 * 版    权 ： 北京爱接力科技有限公司
 * <p/>
 * 作    者 ： iwen
 * <p/>
 * 版    本 ： 1.0
 * <p/>
 * 创建日期 ： on 2016/3/3  16:56
 * <p/>
 * 描    述 ：
 * http请求的封装类，使用 okhttp 框架现实网络请求
 * <p/>
 * <p/>
 * 修订历史 ：
 * <p/>
 * ==========================================
 */
public class GuidesHttpHelper {

    private static final String TAG = "ehome";

    /**
     * 获取验证码的网络操作
     * 链接超时时间：5000
     * 读取超时时间：5000
     *
     * @param verifyCodeRequest 手机数据类型
     * @param guidesPresenter   控制器对象
     */
    public static void post2GetVerifycode(VerifyCodeRequest verifyCodeRequest, final IGuidesPresenter guidesPresenter) {

        Log.d(TAG, "开始获取短信验证码操作,手机号：" + verifyCodeRequest.mobile);
        String url = "http://10.200.0.126/eHome/sendVerifyCode.json";
        OkHttpUtils
                .postString()
                .url(url)
                .content(new Gson().toJson(verifyCodeRequest))
                .build()
                .connTimeOut(5000)
                .readTimeOut(5000)
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                        guidesPresenter.getVerifyCodeError(e.getMessage());
                    }

                    @Override
                    public void onResponse(String response) {
                        guidesPresenter.getVerifyCodeSuccess(new Gson().fromJson(response, VerifyCodeResponse.class));
                    }
                });
    }


    /**
     * 手机登录注册,微信的网络操作
     * 链接超时时间：5000
     * 读取超时时间：5000
     *
     * @param registRequest
     * @param guidesPresenter
     */
    public static void post2PhoneAndWeixinRegist(RegistRequest registRequest, final IGuidesPresenter guidesPresenter) {

        final String type = registRequest.type;
        String message = "";

        // 手机注册
        if ("2".equals(type)) {
            message = "开始手机登录注册,参数：";
        }

        // 微信注册
        if ("1".equals(type)) {
            message = "开始微信登录注册,参数：";
        }

        Log.d(TAG, message + registRequest.toString());
        String url = "http://10.200.0.126/eHome/login.json";
        OkHttpUtils
                .postString()
                .url(url)
                .content(new Gson().toJson(registRequest))
                .build()
                .connTimeOut(5000)
                .readTimeOut(5000)
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e) {

                        // 手机注册
                        if ("2".equals(type)) {
                            guidesPresenter.phoneRegistError(e.getMessage());
                            return;
                        }

                        // 微信注册
                        if ("1".equals(type)) {
                            guidesPresenter.weixinRegistError(e.getMessage());
                            return;
                        }
                    }

                    @Override
                    public void onResponse(String response) {

                        // 手机注册
                        Log.i(TAG, "手机注册返回gson结果集：" + response);
                        if ("2".equals(type)) {
                            guidesPresenter.phoneRegistSuccess(new Gson().fromJson(response, RegistResponse.class));
                            return;
                        }

                        // 微信注册
                        Log.i(TAG, "微信注册返回gson结果集：" + response);
                        if ("1".equals(type)) {
                            guidesPresenter.weixinRegistSuccess(new Gson().fromJson(response, RegistResponse.class));
                            return;
                        }
                    }
                });
    }

}
